package com.nilcaream.dirstore.core.model;

/**
 * TODO
 * <p/>
 * Krzysztof Smigielski 7/13/13 9:07 PM
 */
public class ZipDir implements Comparable<ZipDir> {

    private String zipFileName;
    private String directoryName;
    private final String uri;

    public ZipDir(String zipFileName, String directoryName) {
        this.zipFileName = zipFileName;
        this.directoryName = directoryName;
        uri = zipFileName + "!/" + directoryName;
    }

    public String getZipFileName() {
        return zipFileName;
    }

    public String getDirectoryName() {
        return directoryName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ZipDir zipDir = (ZipDir) o;

        if (directoryName != null ? !directoryName.equals(zipDir.directoryName) : zipDir.directoryName != null)
            return false;
        if (zipFileName != null ? !zipFileName.equals(zipDir.zipFileName) : zipDir.zipFileName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = zipFileName != null ? zipFileName.hashCode() : 0;
        result = 31 * result + (directoryName != null ? directoryName.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ZipDir{");
        sb.append("zipFileName='").append(zipFileName).append('\'');
        sb.append(", directoryName='").append(directoryName).append('\'');
        sb.append('}');
        return sb.toString();
    }

    @Override
    public int compareTo(ZipDir o) {
        return uri.compareTo(o.uri);
    }

    public boolean isRootOf(ZipDir child) {
        return child.uri.startsWith(uri);
    }
}
